<?php

/**
 * This is the model class for table "users".
 *
 * The followings are the available columns in table 'users':
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $email
 * @property string $activkey
 * @property integer $createtime
 * @property integer $lastvisit
 * @property integer $superuser
 * @property integer $status
 * @property integer $modified_time
 * @property string $phone
 * @property string $mobile
 * @property string $avatar
 * @property string $address
 * @property integer $province
 * @property integer $lock_from
 * @property integer $parent
 * @property integer $business
 * @property integer $method_tran
 * @property integer $birthday
 * @property integer $gender
 * @property string $web
 * @property string $full_name
 */
class Users extends CActiveRecord
{
    const STATUS_NOACTIVE=0;
    const STATUS_NOACTIVE_ICO = '/images/fff/time.png';
    const STATUS_ACTIVE=1;
    const STATUS_ACTIVE_ICO = '/images/fff/accept.png';
    const STATUS_BANED=-1;
    const STATUS_BANED_ICO = '/images/fff/lock.png';

    const S_LOCKED=1;
    const S_UNLOCKED=0;

    const S_SUPPLIER = 3;
    const S_TRANSPORTER = 2;
    const S_DISTRIBUTOR = 4;
    const S_DEFAULT_USER = 1;
    const S_ADMINISTRATOR = 5;

    const S_IMAGES = '/media/images/';
    const S_THUMBNAIL = '/media/images/thumbnails/';
    const S_NOIMAGE = 'noimage.gif';
	/**
	 * Returns the static model of the specified AR class.
	 * @return Users the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, password, email', 'required'),
			array('createtime, lastvisit, superuser, status, modified_time, lock_from, birthday, gender, parent, business, method_tran', 'numerical', 'integerOnly'=>true),
			array('username', 'length', 'max'=>20),
			array('password, email, activkey', 'length', 'max'=>128),
			array('phone, mobile', 'length', 'max'=>30),
			array('avatar, address, web, full_name', 'length', 'max'=>255),
            array('province','length', 'max'=> 50),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username, password, email, activkey, createtime, lastvisit, superuser, status, modified_time, phone, mobile, avatar, address, province, lock_from, birthday, gender, web, full_name', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => 'Username',
			'password' => 'Password',
			'email' => 'Email',
			'activkey' => 'Activkey',
			'createtime' => 'Createtime',
			'lastvisit' => 'Lastvisit',
			'superuser' => 'Superuser',
			'status' => 'Status',
			'modified_time' => 'Modified Time',
			'phone' => 'Phone',
			'mobile' => 'Mobile',
			'avatar' => 'Avatar',
			'address' => 'Address',
			'province' => 'Province',
			'lock_from' => 'Lock From',
			'birthday' => 'Birthday',
			'gender' => 'Gender',
			'web' => 'Web',
			'full_name' => 'Full Name',
            'parent'    => 'Parent',
            'business' => 'Business',
			'method_tran' => 'Method Tran',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('activkey',$this->activkey,true);
		$criteria->compare('createtime',$this->createtime);
		$criteria->compare('lastvisit',$this->lastvisit);
		$criteria->compare('superuser',$this->superuser);
		$criteria->compare('status',$this->status);
		$criteria->compare('modified_time',$this->modified_time);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('mobile',$this->mobile,true);
		$criteria->compare('avatar',$this->avatar,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('province',$this->province);
		$criteria->compare('lock_from',$this->lock_from);
		$criteria->compare('birthday',$this->birthday);
		$criteria->compare('gender',$this->gender);
		$criteria->compare('web',$this->web,true);
		$criteria->compare('full_name',$this->full_name,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
    
    public function searchDistributor(){
        $criteria=new CDbCriteria;
        
        $criteria->join = 'INNER JOIN distributor_product ON distributor_product.distributor_id=t.id';
        $criteria->compare('supplier_id', Yii::app()->user->id);
		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('activkey',$this->activkey,true);
		$criteria->compare('createtime',$this->createtime);
		$criteria->compare('lastvisit',$this->lastvisit);
		$criteria->compare('superuser',$this->superuser);
		$criteria->compare('status',$this->status);
		$criteria->compare('modified_time',$this->modified_time);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('mobile',$this->mobile,true);
		$criteria->compare('avatar',$this->avatar,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('province',$this->province);
		$criteria->compare('lock_from',$this->lock_from);
		$criteria->compare('birthday',$this->birthday);
		$criteria->compare('gender',$this->gender);
		$criteria->compare('web',$this->web,true);
		$criteria->compare('full_name',$this->full_name,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
    }
}